﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace BskLeague.Controllers
{
    [Authorize(Roles = "Admin")]
    public class FilesController : Controller
    {
        //
        // GET: /Files/

        public ActionResult Index()
        {
            var filesList1 = Directory.GetFiles(Server.MapPath("/PDFs/")).ToList();
            List<FileInfo> filesList = new List<FileInfo>();
         
            List<string> filesNames = new List<string>();
            foreach (var f in filesList1)
            {
                filesList.Add(new FileInfo(f));
            }
            filesList.OrderBy(x => x.CreationTime);
            return View(filesList);
        }


        public ActionResult GetFile(string name)
        {

            return File("/PDFs/" + name, "application/pdf", name);
        }

        public ActionResult FileGenerated(string name)
        {
            @ViewBag.fileName = name;
            return View();
        }

        public ActionResult DeleteFile(string name)
        {
            
            string FileToDelete;
            FileToDelete = Server.MapPath("/PDFs/"+name);
            System.IO.File.Delete(FileToDelete);
            return RedirectToAction("Index");
        }
    }
}
